package com.audamob.audasingers.tool.db;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.app.Activity;

import com.audamob.audasingers.model.News;
import com.audamob.audasingers.model.Song;
import com.audamob.audasingers.model.Tweet;

public class CacheReadWriter {

	public static ArrayList<News> restore_News(Activity activity)
			throws IOException, ClassNotFoundException {
		FileInputStream fin = new FileInputStream(activity.getCacheDir()
				.getAbsolutePath() + "/newsnews");
		ObjectInputStream in = new ObjectInputStream(fin);
		ArrayList<News> object = (ArrayList<News>) in.readObject();
		in.close();
		return object;
	}

	public static void sauvegard_News(ArrayList<News> s, Activity activity)
			throws IOException {
		FileOutputStream fout = new FileOutputStream(activity.getCacheDir()
				.getAbsolutePath() + "/newsnews");
		ObjectOutputStream out = new ObjectOutputStream(fout);

		out.writeObject(s);
		out.close();
	}

	public static ArrayList<Tweet> restoreTweets(Activity activity)
			throws IOException, ClassNotFoundException {
		FileInputStream fin = new FileInputStream(activity.getCacheDir()
				.getAbsolutePath() + "/twweets");
		ObjectInputStream in = new ObjectInputStream(fin);
		ArrayList<Tweet> object = (ArrayList<Tweet>) in.readObject();
		in.close();
		return object;
	}

	public static void sauvegardTweets(ArrayList<Tweet> s, Activity activity)
			throws IOException {
		FileOutputStream fout = new FileOutputStream(activity.getCacheDir()
				.getAbsolutePath() + "/twweets");
		ObjectOutputStream out = new ObjectOutputStream(fout);

		out.writeObject(s);
		out.close();
	}

	

	public static ArrayList<Song> restoreFavoriteList(Activity activity) throws IOException,
			ClassNotFoundException {
		FileInputStream fin = new FileInputStream(activity.getCacheDir()
				.getAbsolutePath() + "/favoritesong");
		ObjectInputStream in = new ObjectInputStream(fin);
		ArrayList<Song> object = (ArrayList<Song>) in.readObject();
		in.close();
		return object;
	}
	public static void sauvegardFavoriteList(ArrayList<Song> s,Activity activity)
			throws IOException {
		FileOutputStream fout = new FileOutputStream(activity.getCacheDir().getAbsolutePath()
				+ "/favoritesong");
		ObjectOutputStream out = new ObjectOutputStream(fout);

		out.writeObject(s);
		out.close();
	}
}
